package cn.school.sys.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.school.sys.dao.AcademicYearDao;
import cn.school.sys.entity.AcademicYear;
import cn.school.sys.service.AcademicYearService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AcademicYearServiceImpl extends ServiceImpl<AcademicYearDao, AcademicYear> implements AcademicYearService {
    @Override
    public List<AcademicYear> getList(Map<String, Object> params) {
        LambdaQueryWrapper<AcademicYear> wrapper = new LambdaQueryWrapper<>();
        List<AcademicYear> list = this.baseMapper.selectList(wrapper);
        if (list != null && list.size() !=0 ) {
            // 当前学年赋值
            for (AcademicYear academicYear : list) {
                Date sysDate = Calendar.getInstance().getTime();
                Date beginYear = DateUtil.parse("" + academicYear.getAcyeBeginYear(), "yyyy");
                Date endYear = DateUtil.parse("" + academicYear.getAcyeEndYear() + "-12-31 23:59:59", "yyyy-MM-dd HH:mm:ss");
                if (beginYear.getTime() <= sysDate.getTime() && sysDate.getTime() <= endYear.getTime()) {
                    academicYear.setCurrent(true); // 当前学年
                    break;
                }
            }
        }
        return list;
    }

    @Override
    public AcademicYear getById(Long id) {
        AcademicYear academicYear = this.baseMapper.selectById(id);
        return academicYear;
    }
}
